package com.mathpractise.controller;

import com.mathpractise.dto.request.AnswerQuestionRequestDTO;
import com.mathpractise.dto.request.StartTrainingRequestDTO;
import com.mathpractise.dto.response.AnswerQuestionResponseDTO;
import com.mathpractise.dto.response.FinishTrainingResponseDTO;
import com.mathpractise.dto.response.StartTrainingResponseDTO;
import com.mathpractise.dto.response.QuestionTypeInfoDTO;
import com.mathpractise.service.TrainingService;
import com.mathpractise.model.enums.QuestionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 训练控制器
 */
@RestController
@RequestMapping("/api/training")
public class TrainingController {
    
    private static final Logger logger = LoggerFactory.getLogger(TrainingController.class);
    
    @Autowired
    private TrainingService trainingService;
    
    /**
     * 获取所有可用的题型信息
     *
     * @return 题型信息列表
     */
    @GetMapping("/question-types")
    public ResponseEntity<ApiResponse<List<QuestionTypeInfoDTO>>> getAvailableQuestionTypes() {
        logger.info("获取所有可用题型信息");
        List<QuestionTypeInfoDTO> questionTypes = new ArrayList<>();
        
        for (QuestionType type : QuestionType.values()) {
            QuestionTypeInfoDTO dto = new QuestionTypeInfoDTO();
            dto.setName(type.name());
            
            switch (type) {
                case ONE_DIGIT_ADDITION:
                    dto.setLabel("1位数 + 1位数");
                    break;
                case TWENTY_WITHIN_ADD_SUB:
                    dto.setLabel("20以内加减法");
                    break;
                case TWO_DIGIT_ADD_SUB_ONE_DIGIT:
                    dto.setLabel("两位数加减1位数");
                    break;
                default:
                    dto.setLabel(type.name());
                    break;
            }
            
            questionTypes.add(dto);
        }
        
        return ResponseEntity.ok(ApiResponse.success(questionTypes));
    }
    
    /**
     * 开始训练接口
     *
     * @param request 开始训练请求DTO
     * @return 开始训练响应DTO
     */
    @PostMapping("/start")
    public ResponseEntity<ApiResponse<StartTrainingResponseDTO>> startTraining(@RequestBody StartTrainingRequestDTO request) {
        logger.info("开始训练请求: moduleType={}, questionType={}", 
            request.getModuleType(), request.getTrainingConfig().getQuestionType());
        StartTrainingResponseDTO response = trainingService.startTraining(request);
        logger.info("训练开始成功，会话ID: {}", response.getSessionId());
        return ResponseEntity.ok(ApiResponse.success(response));
    }
    
    /**
     * 答题接口
     *
     * @param request 答题请求DTO
     * @return 答题响应DTO
     */
    @PostMapping("/answer")
    public ResponseEntity<ApiResponse<AnswerQuestionResponseDTO>> answerQuestion(@RequestBody AnswerQuestionRequestDTO request) {
        logger.info("答题请求: sessionId={}, questionId={}, userAnswer={}", 
            request.getSessionId(), request.getQuestionId(), request.getUserAnswer());
        AnswerQuestionResponseDTO response = trainingService.answerQuestion(request);
        logger.info("答题处理完成: sessionId={}, questionId={}", request.getSessionId(), request.getQuestionId());
        return ResponseEntity.ok(ApiResponse.success(response));
    }
    
    /**
     * 结束训练接口
     *
     * @param sessionId 会话ID
     * @return 结束训练响应DTO
     */
    @PostMapping("/finish")
    public ResponseEntity<ApiResponse<FinishTrainingResponseDTO>> finishTraining(@RequestParam String sessionId) {
        logger.info("结束训练请求: sessionId={}", sessionId);
        FinishTrainingResponseDTO response = trainingService.finishTraining(sessionId);
        logger.info("训练结束成功: sessionId={}", sessionId);
        return ResponseEntity.ok(ApiResponse.success(response));
    }
}